<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class laporan_stok extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_laporan_stok');

		//library breadcrum/untuk navigasi
		$this->load->library('breadcrumb');		
	}

	function index() {
		//rule type pada function ini: Read
		$this->rule->type('R');

    //Menghapus session pencarian stok obat
    $this->session->unset_userdata('keyword_stok');

		//paging dan ambil data dari model
		$this->load->library('paging');                         		
		//Load library "paging"
		$config['sql']      				= $this->m_laporan_stok->get();
    //Query SQL - bukan array lho ya, liat model
		$config['per_page'] 				= 15;                            
    //jumlah item tiap halaman
		$data = $this->paging->create_pagination($config);   				
    //Load fungsi di library "paging" untuk membuat pagination. 

		//menghilangkan session jika ada
		$this->session->unset_userdata('keyword');

		//judul halaman
		$judul          = 'Laporan Stok';
		$data['judul']  = $judul;
		
    // Title HTML
		$this->layout->set_title($judul);

    //JS Select2
    $this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
    $this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

    //breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Laporan', site_url('laporan_stok#'));
		$this->breadcrumb->add_crumb($judul);

    //Ambil data kelompok
    $data['kelompok'] = $this->m_laporan_stok->get_kelompok();
    //Mendeklarasikan Kode Kelompok
    $data['KdKelompok'] = "";
		//menggunakan theme back
		$this->layout->theme('apotek','laporan_stok/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//buat cari
		if(isset($_POST['keyword'])){
			$data['keyword'] = $this->input->post('keyword');
			$this->session->set_userdata('keyword_stok', $data['keyword']);
		} else {
			$data['keyword'] = $this->session->userdata('keyword_stok');
		}	

    //Mengambil KdKelompok pencarian
    $KdKelompok = $data['keyword'];

    if($KdKelompok != ''){
  		//paging dan ambil data dari model
  		$this->load->library('paging');                         		//Load the "paging" library
      $config['sql']      				= $this->m_laporan_stok->get_cari($data['keyword']);//your SQL - bukan array lho ya...
      $config['per_page'] 				= 5;                            //Display items per page
      $data = $this->paging->create_pagination($config);   				//Load function in "paging" libraryfor create pagination. 
    }else{
      //paging dan ambil data dari model
      $this->load->library('paging');                             //Load the "paging" library
      $config['sql']              = $this->m_laporan_stok->get(); //your SQL - bukan array lho ya...
      $config['per_page']         = 5;                            //Display items per page
      $data = $this->paging->create_pagination($config);          //Load function in "paging" libraryfor create pagination. 
    }

    //Ambil data kelompok
    $data['kelompok']   = $this->m_laporan_stok->get_kelompok();
    //Ambil keyword pencarian
    if($KdKelompok != ''){
      $kata_kunci       = $this->m_laporan_stok->get_kelompok_by($KdKelompok);
    }else{
      $kata_kunci       = "Semua Obat";
    }

    //Mendeklarasikan Kode Kelompok
    $data['KdKelompok'] = $KdKelompok;
    //judul halaman
    $judul              = 'Pencarian stok " <font color="#FC771F">' . $kata_kunci . '</font> "';
    $data['judul']      = $judul;
    // Title HTML
    $this->layout->set_title($judul);
    //meta description jika perlu
    $this->layout->set_desc('Made with love by Raksa Abadi Informatika');
    //breadcrumb/untuk navigasi
    $this->breadcrumb->clear();
    $this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
    $this->breadcrumb->add_crumb('Laporan', site_url('laporan_stok#'));
    $this->breadcrumb->add_crumb('Laporan Stok', site_url('laporan_stok'));
    $this->breadcrumb->add_crumb($judul);

		//menggunakan theme back
    $this->layout->theme('apotek','laporan_stok/index', $data);
  }

  function excel(){
  	$this->rule->type('R');
		//load our new PHPExcel library
  	$this->load->library('excel');
		//activate worksheet number 1
  	$this->excel->setActiveSheetIndex(0);
		//name the worksheet
  	$this->excel->getActiveSheet()->setTitle('Laporan Stok');
		//set cell A1 content with some text
    $KdKelompok   = $this->session->userdata('keyword_stok');
    if($KdKelompok != ''){
      $kelompok   = $this->m_laporan_stok->get_kelompok_by($KdKelompok);
      $this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Stok Obat Berdasarkan Kelompok '.$kelompok.'');
    }else{
  	  $this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Stok Obat');
    }

  	$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
  	$this->excel->getActiveSheet()->setCellValue('B2', 'Nama Obat');
  	$this->excel->getActiveSheet()->setCellValue('C2', 'Kelompok');
  	$this->excel->getActiveSheet()->setCellValue('D2', 'Stok');
		//change the font size
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		//make the font become bold
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
  	$this->excel->getActiveSheet()->mergeCells('A1:D1');
		//set aligment to center for that merged cell (A1 to D1)
  	$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		//set style
  	$TitleStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '00994C') // hijau tua
            ),
  		'font'  => array(
  			'bold'  => false,
  			'color' => array('rgb' => 'FFFFFF'),
  			'size'  => 15,
  			'name'  => 'Verdana'
  			)
  		);

  	$HeaderStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '32CD32') // hijau
            )
  		);

  	$ContentStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFF00') // kuning
            )
  		);

  	$this->excel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($TitleStyle);
  	$this->excel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($HeaderStyle);

		//isi
    if($KdKelompok == ''){
      $list_data    = $this->m_laporan_stok->get_export_all();
    }else{
      $list_data    = $this->m_laporan_stok->get_export_by($KdKelompok);
    }

  	$cols = 3;
  	$num = 1;
  	foreach($list_data as $row){
      $stok = $row['beli'] - $row['jual'] - $row['rusak'];
  		$col = $cols++;
  		$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
  		$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['NmObat']);
  		$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['NmKelompok']);
  		$this->excel->getActiveSheet()->setCellValue('D' . $col, $stok);
  	}

		//warna
  	$cols2 = 3;
  	foreach($list_data as $row){
  		$col2 = $cols2++;
  		$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
  	}

  	$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);


		$filename='laporan stok.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

}
